// SlagCar/DailyCheckStatistics/DailyCheckStatistics.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    yearOptions: [],
    monthOptions: [],
    dayOptions: [],
    year: '',
    month: '',
    day: '',

    countData: {}
  },

  //获取日期参数
  getDateOptions(){
    let today = new Date(),
      year = today.getFullYear(),
      month = today.getMonth() + 1,
      day = today.getDate(),
      yearOptions = [],
      monthOptions = [];
    today.setDate(1); //设为日期一号
    today.setMonth(month);  //设为下月
    today.setDate(0);   //设为本月最后一日

    let lastDay = today.getDate();//本月最后一天
    for (let y = 2023;y <= year; y++){
      yearOptions.push( { text: y, value: y },)
    }
    for (let m = 1;m <=12;m++){
      monthOptions.push({text: m + '月', value: m })
    }
    this.setData({year,month,day,yearOptions,monthOptions});
    this.setDayOptions();
  },
  //日期选择变化
  onSwitchChange(e){
    let {detail,currentTarget} = e,
      {field} = currentTarget.dataset;
    this.setData({[field]: detail,list: []});
    if (field !== 'day') this.setDayOptions();
    this.getStatistics();
  },
  //获取每月天数选项
  setDayOptions(){
    let {year,month,day} = this.data,
      date = new Date(`${year}/${month}/01`),
      dayOptions = [];
    date.setMonth(month)
    date.setDate(0);
    let lastDay = date.getDate();
    for (let d = 1;d <= lastDay;d++){
      dayOptions.push({text: d + '日', value: d })
    }
    let obj = {dayOptions}
    if (day && day > lastDay) obj.day = lastDay
    this.setData(obj)
  },

  //获取统计数据
  getStatistics(){
    let {year,month,day} = this.data,
      url = `ZTAuth/OneDayThreeCheckStat/${year}-${month}-${day}`,
      obj = {};
    wx.showLoading({mask: true});
    wx.$get(5,url).then(res => {
      if (res.errcode != 0) return;
      wx.hideLoading();
      if (res.data.row){
        let rows = res.data.row,
          data = rows[0] || {};
        this.setData({countData: data});
      }
    })
  },

  //跳转列表页面
  navTo(e){
    let {type} = e.currentTarget.dataset,
      {year,month,day} = this.data;
    wx.navigateTo({url: `/SlagCar/DailyCheckAdmin/DailyCheckAdmin?type=${type}&date=${year}/${month}/${day}`})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDateOptions();
    this.getStatistics();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
